﻿using Google.Protobuf;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;

public class NewBehaviourScript33 : MonoBehaviour {

    string clsassName = "Pb.Api2";
    //public delegate void Callback( arg1);
    //public delegate void Callback<T>(T arg1);


    //private List<Callback> Callback = List<Action<>>
    //private Dictionary<string, List<Action<object>>> fundic = new Dictionary<string, List<Action<object>>>();

    //private Callback<object> callback;
    // Use this for initialization
    void Start () {
        byte[] bytes;
        var ss = new Pb.Api2();
        var tt = new Pb.Api();
        tt.Name.Add(1111);
        ss.Obj = tt;
       
        bytes = ss.ToByteArray();
        Debug.Log("bytes.Length:" + bytes.Length);
        
        Assembly assembly = Assembly.GetExecutingAssembly();
        var cls = assembly.GetType(clsassName);
        var parserProp = cls.GetProperty("Parser");
        dynamic parser = parserProp.GetValue(null);
        var news = parser.ParseFrom(bytes);


        register<Pb.Api2>(clsassName, Dispatch);

        Messenger.Broadcast(clsassName, news);
    }

    private void register<T>(string tag , Callback<T> fun)
    {
        Messenger.AddListener(tag, fun);
    }

    private void Dispatch(Pb.Api2 news)
    {
        for (int i = 0; i < news.Obj.Name.Count; i++)
        {
            Debug.Log(i + "<<<" + news.Obj.Name[i]);
        }
    }

    // Update is called once per frame
    void Update () {
		
	}
}
